<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="会员ID" prop="memberId">
        <el-input
          v-model="queryParams.memberId"
          placeholder="请输入会员ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="场地ID" prop="courtId">
        <el-input
          v-model="queryParams.courtId"
          placeholder="请输入场地ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间">
        <el-date-picker
          v-model="daterangeStartTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['manager:reservation:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manager:reservation:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manager:reservation:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['manager:reservation:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reservationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" type="index" width="55"/>

      <el-table-column label="会员姓名" align="center" width="100" prop="memberId" >
<!--        从memberList中获取memberId相等的用户 显示用户姓名-->
        <template v-slot="scope">
          <!--      策略类型  策略id strategyId  循环策略表 strategyList 根据策略id找到策略名字 name-->
          <el-tag v-for="item in memberList" v-if="item.id === scope.row.memberId">{{item.name}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="场地编号" align="center" prop="courtId" width="110" >
        <template v-slot="scope">
          <el-tag v-for="item in placeList" v-if="item.id === scope.row.courtId">{{item.number}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预约时长" align="center" prop="duration" width="80">
        <template v-slot="scope">
          <span>{{ scope.row.duration }}分钟</span>
        </template>
      </el-table-column>
      <el-table-column label="使用人数" align="center" prop="peopleCount" width="80" />
      <el-table-column label="状态" align="center" prop="status" width="70">
        <template v-slot="scope">
          <el-tag v-if="scope.row.status === 0" size="small">未使用</el-tag>
          <el-tag v-else-if="scope.row.status === 1" size="small" type="success">使用中</el-tag>
          <el-tag v-else-if="scope.row.status === 2" size="small" type="info">已完成</el-tag>
          <el-tag v-else-if="scope.row.status === 3" size="small" type="warning">已取消</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="实际支付金额" align="center" prop="actualFee">
        <template v-slot="scope">
          <span>{{ scope.row.actualFee / 100 }}元</span>
        </template>
      </el-table-column>
      <el-table-column label="预约创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="notes"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预约管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="会员ID" prop="memberId">
          <el-input v-model="form.memberId" placeholder="请输入会员ID"/>
        </el-form-item>
        <el-form-item label="场地ID" prop="courtId">
          <el-input v-model="form.courtId" placeholder="请输入场地ID"/>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
                          v-model="form.startTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="使用人数" prop="peopleCount">
          <el-input v-model="form.peopleCount" placeholder="请输入使用人数"/>
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input v-model="form.notes" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listReservation,
  getReservation,
  delReservation,
  addReservation,
  updateReservation
} from "@/api/manager/reservation"

import {getMember, listMember} from "@/api/manager/member";
import {listPlace} from "@/api/manager/place";

export default {
  name: "Reservation",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预约管理表格数据
      reservationList: [],
      // 所有会员信息
      memberList: [],
      // 所有场地信息
      placeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeStartTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberId: null,
        courtId: null,
        startTime: null,
        duration: null,
        status: null,
      },
      //查询参数
      commonQueryParams:{
        pageNum: 1,
        pageSize: 1000
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        startTime: [
          {required: true, message: "开始时间不能为空", trigger: "blur"}
        ],
        duration: [
          {required: true, message: "预约时长(分钟)不能为空", trigger: "change"}
        ],
      }
    }
  },
  created() {
    this.getList()
    this.getMemberList()
    this.getPlaceList()
  },
  methods: {
    /** 查询预约管理列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeStartTime && '' != this.daterangeStartTime) {
        this.queryParams.params["beginStartTime"] = this.daterangeStartTime[0]
        this.queryParams.params["endStartTime"] = this.daterangeStartTime[1]
      }
      listReservation(this.queryParams).then(response => {
        this.reservationList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /*获取会员列表*/
    getMemberList(){
      this.loading = true
      listMember(this.commonQueryParams).then(response => {
        this.memberList = response.rows
        console.log(this.memberList)
        this.loading = false
      })
    },
    /*获取场地列表*/
    getPlaceList(){
      this.loading = true
      listPlace(this.commonQueryParams).then(response => {
        this.placeList = response.rows
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        memberId: null,
        courtId: null,
        startTime: null,
        duration: null,
        peopleCount: null,
        status: null,
        actualFee: null,
        createTime: null,
        notes: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeStartTime = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加预约管理"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getReservation(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改预约管理"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReservation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addReservation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除预约管理编号为"' + ids + '"的数据项？').then(function () {
        return delReservation(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('manager/reservation/export', {
        ...this.queryParams
      }, `reservation_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
